import { createRouter,createWebHashHistory } from 'vue-router'
import { CONFIG } from '../config/index.js'
import userRoutes from './user.js'

const Login = () => import('../view/Login.vue')
// const Index = () => import('../view/Index.vue')
const Layout = ()=>import ('../view/layout/Layout.vue')

const routes = [
    {
        path: "/login",
        component: Login,
    },
    {
        path: "/",
        component: Layout
    },
    userRoutes
]

const router = createRouter({
    history:createWebHashHistory(),
    routes,
})
router.beforeEach(
    (to,from,next) =>{
        const toPath = to.path
        const toLogin = toPath.indexOf("/login")
        const tokenStatus = window.localStorage.getItem(CONFIG.TOKEN_NAME)
        if (toLogin==0 && tokenStatus){
            next("/")
        }else if(toLogin ==0 && !tokenStatus){
            next()
        }else if(tokenStatus){
            next()
        }else {
            next("/login")
        }
    }
)
export default router